package com.upcode.weighing.admin.controller;


import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.upcode.weighing.base.controller.BaseController;
import com.upcode.weighing.common.util.StringUtils;
import com.upcode.weighing.domain.AppInfo;
import com.upcode.weighing.exception.AdminHandlerException;
import com.upcode.weighing.service.AppInfoService;
import com.upcode.weighing.vo.req.AppEidtionRecordReqVo;
import com.upcode.weighing.vo.req.PageReqVo;
import com.upcode.weighing.vo.req.pc.AppEidtionRecordVo;
import com.upcode.weighing.vo.resp.PageRespVo;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author alex
 * @since 2024-07-21
 */
@Controller
@RequestMapping("/appinfo")
public class AppInfoController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(AppInfoController.class);

    @Value("${handheld.serverUrl}")
    private String serverUrl;

    @Value("${handheld.profile}")
    private String profile;

    @Value("${handheld.appPackDownloadPath}")
    private String appPackDownloadPath;

    @Autowired
    private AppInfoService appInfoService;

    @ApiOperation(value = "APP版本列表", notes = "")
    @PostMapping(value = "/queryPageList")
    public Object queryPageList( @Validated @RequestBody PageReqVo<AppEidtionRecordReqVo> vo){
        try {
            PageRespVo<AppEidtionRecordVo> page = appInfoService.queryPageList(vo,appPackDownloadPath);
            return success(page);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return error("查询列表失败");
        }
    }

    @ApiOperation(value = "添加APP版本", notes = "")
    @PostMapping(value = "/saveRecord")
    public Object save(@Validated @RequestBody AppInfo vo){
        try{
            if(null == vo){
                return error("保存信息不可为空");
            }
            vo.setId(IdWorker.getId());
            vo.setStatus(1);//初始版本为停用
            boolean result = appInfoService.save(vo);
            if(!result){
                return error("保存失败");
            }
            return success("保存成功");
        }catch (Exception e){
            logger.error("保存APP版本异常",e.getMessage(),e);
            return error("保存异常");
        }
    }

    @ApiOperation(value = "更新APP版本信息", notes = "")
    @PostMapping(value = "/updateRecord")
    public Object updateRecord(@Validated @RequestBody AppInfo vo){
        try{
            if(null!=vo.getId()){
                return error("ID不可为空");
            }
            AppInfo updateData = appInfoService.getById(vo.getId());
            if(null == updateData){
                return error("APP版本信息不存在");
            }
            boolean result = appInfoService.updateById(vo);
            if(!result){
                return error("更新失败");
            }
            return success("更新成功");
        }catch (Exception e){
            logger.error("更新APP版本异常",e.getMessage(),e);
            return error("更新异常");
        }
    }

    @ApiOperation(value = "启用禁用APP版本", notes = "status 0启用 1禁用")
    @GetMapping(value = "/enableAppEidtion/{id}/{status}")
    public Object enableAppEidtion(@PathVariable("id") String id, @PathVariable("status") String status){
        try {
            if (StringUtils.isEmpty(id) || StringUtils.isEmpty(status)) {
                return error("参数不合法");
            }
            AppInfo updateData = appInfoService.getById(id);
            if (null == updateData) {
                return error("APP版本信息不存在");
            }
            boolean result = appInfoService.enableAppEidtion(id, status, serverUrl, profile);
            if (!result) {
                return error("操作失败");
            }
            return success("操作成功");
        } catch (AdminHandlerException e) {
            logger.error("启用/禁用APP版本异常", e.getMessage(), e);
            return error(e.getMessage());
        } catch (Exception e) {
            logger.error("启用/禁用APP版本异常", e.getMessage(), e);
            return error("操作异常");
        }
    }
    @ApiOperation(value = "删除APP版本", notes = "")
    @GetMapping(value = "/delAppEidtion/{id}")
    public Object delAppEidtion(@PathVariable("id") String id){
        try{
            if(StringUtils.isEmpty(id)){
                return error("参数不合法");
            }
            boolean result = appInfoService.deleteAppEidtion(id);
            if(!result){
                return error("删除失败");
            }
            return success("删除成功");
        }catch (Exception e){
            logger.error("启用/禁用APP版本异常",e.getMessage(),e);
            return error("操作异常");
        }
    }
}
